package com.neural.randomizer;

import java.util.Random;

public class Randomizer {


	private final Range range; 
	private final double rangeSize;
	
	private final int tabSize;	
	private final Random randomizer;
	
	private final int intTab[];
	private final long longTab[];
	private final double doubleTab[];
	
	public Randomizer(Range range, int tableSize) {
		
		this.range = range;
		this.rangeSize = range.getRight() - range.getLeft();
		
		this.tabSize = tableSize;
		this.randomizer = new Random(System.nanoTime());
		
		this.intTab = new int[tableSize];
		this.longTab = new long[tableSize];
		this.doubleTab = new double[tableSize];
	}
	
	public int generateInt() {
		for(int i = 0; i < tabSize; i++) {
			intTab[i] = createRandomInt();
		}
		return intTab[randomIndex()];
	}
	
	public long generateLong() {
		for(int i = 0; i < tabSize; i++) {
			longTab[i] = createRandomLong();
		}
		return longTab[randomIndex()];
	}
	
	public double generateDouble() {
		for(int i = 0; i < tabSize; i++) {
			doubleTab[i] = createRandomDouble();
		}
		double i = doubleTab[randomIndex()];
//		System.out.println(i);
		return  i;
	}
	
	private int randomIndex() {
		return Math.abs(randomizer.nextInt()) % tabSize;
	}
	
	private int createRandomInt() {
		return randomizer.nextInt((int)Math.floor(rangeSize)) + (int)Math.round(range.getLeft());
	}
	
	private long createRandomLong() {		
		return (long) ((Math.abs(randomizer.nextLong()) % rangeSize)  + (int)Math.round(range.getLeft()));
	}
	
	private double createRandomDouble() {		
		return Math.abs(randomizer.nextDouble()) * rangeSize + range.getLeft();
	}
}
